C PROGRAMMING EXERCISE 5

Exercise 2: Loop Control Structure

Youtube Video:

 
CODE IS AS FOLLOWS:

    

//Hello and welcome everyone
//Lets start with Exercise 5 but first let me tell u that ex 5 is based on LOOP CONTROL STRUCTURES
//Lets get started
//completinf the code 
#include 
#include 

void main()
{
  int opt,sum=0,m,n,c,temp,i,j,b=1,p,x,fd,ld,num_even=0,num_odd=0,num_0=0;
  clrscr();
  while(1)
  {
   printf("\nEnter your choice\n");
   printf("1.\t2.\t3.\n4.\t5.\t6.\n7.\t8.\t9.\n10.\t11.\t12.\nPress 12 for exit");
   scanf("%d",&opt); 
     switch (opt)
     {
       case 1: printf("Q1:-WRITE A PROGRAM TO CALCULATE SUM OF DIGITS OF A GIVEN INPUT NUMBER");
               printf("\nSolution");
               printf("Enter the digit");
               scanf("%d",&n);
                 while(n>0)
                 {
                   m=n%10;
                   sum=sum+m;
                   n=n/10;
                 }
                 printf("%d is the sum of digit",sum);
                 
               break;
       
       case 2: printf("Q2:-ACCEPT TWO NUMBERS AS RANGE AND DISPLAY SUM OF ALL NUMBERS BETWEEN THAT RANGE");
               printf("\nSolution");
               printf("Enter tow numbers as range");
               scanf("%d%d",&m,&n);
                 for(i=m;i<=n;i++)
                 {
                   sum=sum+i;
                 }
                 printf("%d is sum of all numbers between the ramge",sum);
                 
               break;
       
       case 3: printf("Q3:-WRITE A PROGRAM TO CHECK WHETHER A INPUT IS ARMSTRONG OR NOT");
               printf("\nSolution");
               printf("Enter the number");
               scanf("%d",&n);
               temp=n;
                 while(n>0)
                 {
                   m=n%10;
                   c=m*m*m;
                   sum=sum+c;
                   n=n/10;
                 }
                 //lets check if the number is a armstrong or not
                 if(sum==temp)
                 {
                   printf("Number is armstrong");
                 }
                 else
                 {
                   printf("Not an armstrong");
                 }
                 
               break;
       
       case 4: printf("Q4:-WRITE A PROGRAM TO ACCEPT BINARY NUMBER AND CONVERT IT IN DECIMAL");
               printf("\nSolution");
               printf("Enter binary num");
               scanf("%d",&n);
                 while(n>0)
                   {
                     m=n%10;
                     sum=sum+n*b;
                     n=n/10;
                     b=b*2;
                   }
                   printf("%d is the decimal number",sum);
               break;
       
       case 5: printf("Q5:-WRITE A PROGRAM TO CHECK WHETHER THE INPUT NUMBER IS PERFECT NUMBER OR NOT");
               printf("\nSolution");
               printf("Enter the number");
               scanf("%d",&n);
               temp==n;
                 for(i=1;i0)
               {
                 m=n%10;
                 sum=sum*10+m;
                 n=n/10;
                }       
              if(sum==temp)
            {
             printf("Pallindrome");
            }
            else
            {
            printf("Non palindrome");
            }     
               break;
       
       case 8: printf("Q8:-WRITE A C PROGRAM TO DISPLAY MULTIPLICATION OF TWO INPUT NUMBERS WITHOUT USING * OPERATOR");
               printf("\nSolution");
               printf("Enter two numbers");
                scanf("%d%d",&n,&m);
               for(i=1;i<=m;i++)
             {
            sum=sum+n;
            }
            printf("%d*%d=%d",m,n,sum);
               break;
       
       case 9: printf("Q9:-WRITE A PROGRAM TO CALCULATE SUM OF FIRST AND LAST DIGIT OF A NUMBER");
               printf("\nSolution");
               printf("Enter a number");
               scanf("%d",&n);
                 ld=n%10;  //n%10 gives us the last digit
                 //to find first digit,we will divide n by 10 until n>=10
                 while(n>=10)
                 {
                   n=n/10;
                 }
                 fd=n;
                 //now we have first digit so lets add them
                 sum=fd+ld;
                 //displaying the numbers
                 
                 printf("%d is the sum of first and last number",sum);
                 
               break;
       
       case 10: printf("Q10:-WRITE A PROGRAM TO ACCEPT A NUMBER AND COUNT NUMBER OF EVEN,ODD,ZERO DIGITS WHITHIN THAT NUMBER");
               printf("\nSolution");
               printf("Enter the number");
               scanf("%d",&n);
               //we will use while loop
               //num_odd,num_even,num_0 are new variables
               while(n>0)
               {
                 m=n%10;
                 //now we will use nested if else statement
                 
                 if(m==0)
                 {
                   num_0++;  
                 }
                 else if(m%2==0)
                 {
                   num_even++;
                 }
                 else
                 {
                   num_odd++;
                 }
                 n=n/10;
               }
               printf("even number digits except 0=%d\nodd number digits=%d\nnumber of zero digit=%d",num_even,num_odd,num_0);
               break;
               
       
       case 11: printf("Q11:-WRITE A C PROGRAM WHICH ACCEPTS A NUMBER N AND DISPLAYS EACH DIGIT IN WORDS");
               printf("\nSolution");
               printf("Enter number");
               scanf("%d",&n);
               
               while(n>0)
               {
               m=m*10+n%10;
               n=n/10;
               }
               while(m>0)
               {
                 p=m%10;
                 //rem=rem/10;
               switch(p)
               {
                 case 1: printf(" one");
                         
                         break;
                         
                 case 2: printf(" two");
                         
                         break;
                         
                 case 3: printf(" three");
                         
                         break;
                         
                 case 4: printf(" four");
                         
                         break;
                         
                 case 5: printf(" five");
                         
                         break;
                         
                 case 6: printf(" six");
                         
                         break;
                         
                 case 7: printf(" seven");
                         
                         break;
                         
                 case 8: printf(" eight");
                         
                         break;
                         
                 case 9: printf(" nine");
                         
                         break;
                         
                }
                         } 
               break;
       
       case 12:exit(1);
     }
     //completed coding....time to jump to check errors...yes jumping to another app :)
    //okay done...stay tuned my future programmer;)
   }  
  getch();
}



    
 Download code